﻿using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.SharePoint;

namespace Sapphire.Linq.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            const string url = "http://vserv/sites/portal/";
            using (var site = new SPSite(url))
            {
                using (var web = site.OpenWeb())
                {
                    using (var dataContext = new TasksDataContext(web))
                    {
                        TestGenerator.EnsureTestData(dataContext.Web);

                        SimpleQueries(dataContext);
                    }
                }
            }
        }

        private static void SimpleQueries(TasksDataContext dataContext)
        {
            var items1 = dataContext.Items.ToList();

            var pagedItems = dataContext.Items.OrderByDescending(x => x.ID).Skip(20).Take(10).ToList();

            Debug.Assert(pagedItems.FirstOrDefault().ID == 80 && pagedItems.Count == 10);

            var orderedItems = dataContext.Items.OrderBy(x => x.Tasks).ToList();

            var orderedDescItems = dataContext.Items.Where(i => i.ID < 10).OrderByDescending(x => x.ID).ToList();

            var itemsCountMoreThan30 = dataContext.Items.Count(i => i.ID > 30);

            Debug.Assert(itemsCountMoreThan30 == 70);

            var itemsCount = dataContext.Items.Count();

            Debug.Assert(itemsCount == 100);

            var firstItem = dataContext.Items.First();

            Debug.Assert(firstItem != null);

            var tasks = firstItem.Tasks.ToList();

            Debug.Assert(tasks.Count() == 9);

            var firstItemWithIdMoreThan1 = dataContext.Items.First(i => i.ID > 1);

            Debug.Assert(firstItemWithIdMoreThan1.ID == 2);

            var containsItems = dataContext.Items.Where(i => i.Title.Contains("New")).ToList();

            Debug.Assert(containsItems.Count() == 100);

            var containsItemsAndIdMore95 = dataContext.Items.Where(i => i.Title.Contains("New") && i.ID > 95).ToList();

            Debug.Assert(containsItemsAndIdMore95.Count() == 5);

            var containsItemsAndIdMore95OrLessThan10 = dataContext.Items.Where(i => i.Title.Contains("New") && i.ID > 95 || i.ID < 5).ToList();

            Debug.Assert(containsItemsAndIdMore95OrLessThan10.Count() == 9);

            var notNullsItems = dataContext.Items.Where(i => i.Title != null).ToList();

            Debug.Assert(notNullsItems.Count() == 100);

            var isNullsItems = dataContext.Items.Where(i => i.Title == null).ToList();

            Debug.Assert(isNullsItems.Count() == 0);

            var recentItems = dataContext.Items.Where(i => i.Created > DateTime.Now.AddDays(-1)).ToList();

            var items = dataContext.Items.ToList();

            Debug.Assert(items.Count() == 100);

            var moreItems = dataContext.Items.Where(i => i.ID > 30).ToList();

            Debug.Assert(moreItems.Count() == 70);

            var newItems = dataContext.Items.Where(i => i.Title.StartsWith("New")).ToList();

            Debug.Assert(newItems.Count() == 100);

            var item1 = dataContext.Items.FirstOrDefault();

            Debug.Assert(item1 != null);

            var item2 = dataContext.Items.Where(i => i.ID == 1);

            Debug.Assert(item2 != null);

            var task = dataContext.Tasks.FirstOrDefault();

            Debug.Assert(task != null);

            var tasks1 = dataContext.Tasks.ToList();

            Debug.Assert(tasks1.Count() == 100);

            var tasks2 = dataContext.Tasks.Take(2).ToList();

            Debug.Assert(tasks2.Count() == 2);

            var tasks3 = dataContext.Tasks.Where(t => t.Title123 != "Task1" | t.Title123 == "Task2").ToList();

            Debug.Assert(tasks3.Count() == 100);

            int two = 1 + 1;

            var tasks4 = dataContext.Tasks.Where(t => t.ID == two).ToList();

            Debug.Assert(tasks4.Count() == 1);

            var tasks5 = dataContext.Tasks.FirstOrDefault(t => t.ID > 1);

            Debug.Assert(tasks5 != null);

        }
    }
}
